<?php
class isp_perakitan_produk_model extends MY_Model {
	var $model = 'isp_perakitan_produk';
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT	* 
					FROM	isp_perakitan_barang 
					WHERE	(UPPER(kode_jenis_barang) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'id_perakitan_barang' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}

	function GetJSON2()
	{
		$ls_sql	= "	SELECT	*
					FROM	ISP_PRODUK 
					WHERE	id_produk NOT IN(SELECT id_produk FROM ISP_PERAKITAN_BARANG)
					ORDER BY ".(post('sort')=='' ? 'id_produk' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}

	function GetJSON3($id_perakitan_barang)
	{
		$ls_sql	= "	SELECT	*
					FROM	ISP_INVENTORI 
					WHERE kode_inventori NOT IN(SELECT kode_barang FROM ISP_PERAKITAN_BARANG_DETAIL WHERE id_perakitan_barang=$id_perakitan_barang)
					ORDER BY ".(post('sort')=='' ? 'kode_inventori' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}

	function GetJSON_detail($id)
	{
		$ls_sql	= "	SELECT 
					a.id_perakitan_barang_detail,
					a.id_perakitan_barang,
					a.id_inventori,
					a.KODE_BARANG as kode_produk,
					a.BANYAK,
					b.NAMA_INVENTORI,
					b.BANYAK as stock
					 FROM ISP_PERAKITAN_BARANG_DETAIL a
					LEFT JOIN ISP_INVENTORI b
					ON a.KODE_BARANG = b.KODE_INVENTORI
					WHERE id_perakitan_barang = '$id'
					AND (UPPER(a.KODE_BARANG) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.banyak) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'a.KODE_BARANG' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}

	
	function Add($arr_data){
		$this->db->insert('isp_perakitan_barang', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('isp_perakitan_barang', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_kode_produk){
		$result = $this->db->get_where('isp_perakitan_barang', array('id_perakitan_barang' => $as_kode_produk));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_kode_produk)
	{
		$result = $this->db->get_where('isp_perakitan_barang', array('id_perakitan_barang' => $as_kode_produk));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			if(	intval($this->get_data("SELECT COUNT(1) FROM isp_perakitan_barang WHERE id_perakitan_barang= '".$arr_rule['id_perakitan_barang']."'"))<=0)
			{
				return $this->error_delete.' [id: '.$arr_rule['id_perakitan_barang']."]<br />";
			}else{
				$this->db->delete('isp_perakitan_barang', array('id_perakitan_barang' => $arr_rule['id_perakitan_barang']));
				$this->db->delete('isp_perakitan_barang', array('id_perakitan_barang_detail' => $arr_rule['id_perakitan_barang_detail']));
				return '';
			}
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>